﻿using System;
using System.Data;
using System.Linq;
using System.Web.Mvc;
using GioHangViet.Areas.Admin.Models;
using GioHangViet.Models;

namespace GioHangViet.Areas.Admin.Controllers
{ 
    public class LanguageController : Controller
    {
        private GoLungTungModel db = new GoLungTungModel();
        private ProcessAlias processAlias=new ProcessAlias();
        //
        // GET: /Admin/Language/
        public ViewResult Index(int id)
        {
            if (id < 1)
            {
                id = 1;
            }
            ViewBag.CurrentPage = id;
            ViewBag.countRecord = db.Languages.Count();
            ViewBag.Path = "Admin/Language/Index";
            ViewBag.pageSize = Resources.Constant.PageSize;
            var model =
                db.Languages.ToList().Skip((id - 1)*Convert.ToInt32(Resources.Constant.PageSize)).Take(
                    Convert.ToInt32(Resources.Constant.PageSize));
            return View(model);
        }

        // GET: /Admin/Language/Create

        public ActionResult Create()
        {
            var model = new Language{Alias = "new"};
            return View(model);
        } 

        //
        // POST: /Admin/Language/Create

        [HttpPost]
        public ActionResult Create(Language language)
        {
            if (ModelState.IsValid)
            {
                language.Alias = processAlias.CreateAlias(language.Name, "Languages", "Alias");
                db.Languages.Add(language);
                db.SaveChanges();
                return RedirectToAction("Index", new{id = 1});  
            }

            return View(language);
        }
        
        //
        // GET: /Admin/Language/Edit/5
 
        public ActionResult Edit(string id)
        {
            Language language = db.Languages.Find(id);
            return View(language);
        }

        //
        // POST: /Admin/Language/Edit/5

        [HttpPost]
        public ActionResult Edit(Language language)
        {
            if (ModelState.IsValid)
            {
                db.Entry(language).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index", new { id = 1 });
            }
            return View(language);
        }

        //
        // GET: /Admin/Language/Delete/5
 
        public ActionResult Delete(string id)
        {
            var language = db.Languages.Find(id);
            if (language != null)
            {
                db.Languages.Remove(language);
                db.SaveChanges();
                return RedirectToAction("Index", new {id = 1});
            }
            return View("Error");
        }


        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}